/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.drypisy;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.FocusAdapter;
import java.awt.image.BufferedImage;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author Marcin
 */
public class ImagePanel extends JPanel {

    private BufferedImage image = null;
    private int xcord = 0;
    private int ycord = 0;
    
    public ImagePanel() {
    }
    
    public void setImage(BufferedImage img){
        image = img;
        setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
        repaint();
    }
    
    public BufferedImage getImage(){
        return image;
    }
    
    public void setCords(int x, int y){
        xcord = x;
        ycord = y;
    }
    
    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2d;
        if (image != null) {
            try {
                g2d = (Graphics2D) g;
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g2d.drawImage(image, xcord, ycord, null);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "Could not load image ");
            }
        }else{
            g2d = (Graphics2D) g;
            g2d.setBackground(Color.white);
            g2d.clearRect(0, 0, getWidth(), getHeight());
            g2d.setFont(new Font("Serif",Font.ITALIC,30));
            g2d.drawString("Brak załadowanego obrazka", 100, 100);
        }
    }

    public static ImagePanel getSmallImagePanel(BufferedImage img){
        ResizeProcess resize = new ResizeProcess();
        double w = img.getWidth();
        double h = img.getHeight();
        double prop = w/h;
        double nw,nh;
        if(w>350 || h> 350){
            if(w>h){
                nw = 350;
                nh = nw/prop;
            }else{
                nh = 350;
                nw = nh*prop;
            }
        }else{
            nw = w;
            nh = h;
        }
        resize.setNewSize((int)nw, (int)nh);
        BufferedImage image = resize.process(img);
        ImagePanel panel = new ImagePanel();
        panel.setImage(image);
        panel.setSize((int)nw, (int)nh);
        return panel;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
